# SVN FILE: $Id$
#
# Project Name : 
#
# @package -.
# @author $Chirantan V. Rajhans$
# @copyright $$
# @version $New$
# @lastrevision $Date$
# @modifiedby $LastChangedBy$
# @lastmodified $LastChangedDate$
# @filesource $E:/Rubyfun/MyStringAPI.rb$
#

require 'net/http'
require 'rexml/document'
require 'www/delicious'


# *Description*
# * Module that contains classes for delicious and stumbleupon bookmark import
# *Exception*
# * No Exception
module BookmarkImport
  # *Description*
  # * Delicious Bookmark Import Class
  # *Exception*
  # * No Exception
  class Delicious
    # *Description*
    # * Authenticates user's credentials for the delicous account and returns a handle.
    # *Parameters*
    # * username: STRING : Delicious Username
    # * password: STRING : Delicious Password
    # *Returns*
    # * true on success
    # *Exception*
    # * No Exception
    def initialize(username, password)
      @titles = []
      @notes = []
      @tags = []
      @times = []
      @delicious = WWW::Delicious.new(username, password)
    end
    
    # *Description*
    # * Authenticates user's credentials for the delicous account and returns a handle.
    # *Parameters*
    # * username: STRING : Delicious Username
    # * password: STRING : Delicious Password
    # *Returns*
    # * true on success
    # *Exception*
    # * No Exception    
    def allposts
      posts = @delicious.posts_all
      fill_posts(posts)
      true
    end
    
    def posts_with_tag(tag)
      nilall
      posts = @delicious.posts_get(:tag => tag)
      fill_posts(posts)
      true
    end
    
    def recent_posts(tag = nil)
      posts = @delicious.posts_recent(:tag => tag)
      fill_posts(posts)
      true
    end

    def showall
      for i in 0...@titles.size
        puts '---------------------------------------'
        puts 'Title -->' + @titles[i].to_s
        puts 'Notes -->' + @notes[i].to_s
        puts 'Tags -->' + @tags[i]
        puts 'time -->' + @times[i].to_s
      end
    end
      
    private 
    
    def fill_posts(posts)
        posts.each do |post|
        @titles << post.title.to_s
        @notes << post.notes.to_s
        @tags << post.tags.join(', ')
        @times << post.time.to_s
      end
    end
    
    def nilall
      @titles = []
      @notes = []
      @tags = []
      @times = []
    end

    attr_accessor :titles, :notes, :tags, :times
  end
  
  class StumbleUpon
    def initialize(username)
      @titles = []
      @links = []
      @guids = []
      @descriptions = []
      @comments = []
      @proxy = Net::HTTP.Proxy('ptproxy.persistent.co.in','8080','chirantan_rajhans','comeasyouare!23')
      @uri = URI.parse("http://rss.stumbleupon.com/user/#{username}/favorites")
      http = @proxy.new(@uri.host, @uri.port) 
      xml_response =  http.get(@uri.path)
      doc = REXML::Document.new(xml_response.body)
      doc.elements.each('rss/channel/item/title') do |ele|
        @titles << ele.text
      end
      doc.elements.each('rss/channel/item/link') do |ele|
        @links << ele.text
      end
      doc.elements.each('rss/channel/item/guid') do |ele|
        @guids << ele.text
      end
      doc.elements.each('rss/channel/item/description') do |ele|
        @descriptions << ele.text
      end
      doc.elements.each('rss/channel/item/comments') do |ele|
        @comments << ele.text
      end
    end
    
    def showall
      for i in 0...@titles.size
        puts '---------------------------------------------'
        puts 'Title -->' + @titles[i].to_s
        puts 'Link -->' + @links[i].to_s
        puts 'GUID -->' + @guids[i].to_s
        puts 'Description -->' + @descriptions[i].to_s
        puts 'Comments -->' + @comments[i].to_s
      end
    end
    
    attr_accessor :titles, :links, :guids, :descriptions, :comments
  end  
end

#~ su = BookmarkImport::StumbleUpon.new('chirantanrajhans')
#~ su.showall
#~ delicious = BookmarkImport::Delicious.new('chirantan.rajhans', 'me#01123581321')
#~ delicious.recent_posts('css')
#~ delicious.allposts
#~ delicious.showall